package aber.cs12320.week3;

import java.util.Scanner;

/**
 * CS12320 worksheet 10 
 * This worksheet gets you to make an Application class with a menu 
 * to run the clock application
 * @author mwg2
 * @since 12/02/2015
 */
public class Application {
	
	private Clock clock;
	
	public Application() {
		clock = new Clock(new Time(0,0), new Time(1,10), false);
	}
	
	public static void main(String[] args) {
		Application app = new Application();
		app.runMenu();
	}
	
	/**
	 * Allow user to change 5 times current time, alarm time 
	 * and set the alarm ON / OFF
	 */
	public void runMenu() {
		Scanner in = new Scanner(System.in);
		int increment = 0;
		boolean invalidOption = true;
		int option = 0;
		
		do {
			while (invalidOption) {
				System.out.println("What do you want to change?");
				System.out.println("Type\n 1 for current time\n2 for alarm time"
						+ "\n3 to set the alarm ON/OFF");
				
				// Get user answer
				option = in.nextInt();
				if (option < 1 || option > 3) continue;
				
				// Answer valid
				invalidOption = false;
			}
			
			selectOption(option, in);
			increment++;
			invalidOption = true;
			
			// Print the state of the application object
			System.out.println(this);
		} while (increment < 5);
		System.out.println("You have used all 5 chances");
	}	
	
	@Override
	public String toString() {
		return "Application [clock=" + clock + "]";
	}

	/**
	 * Executes a selection of one of the options
	 * @param option Option 1, 2 or 3 selected by user
	 * @param in Scanner object to read input from keyboard
	 */
	private void selectOption(int option, Scanner in) {
		String answer;
		int hours;
		int minutes;
		
		switch (option) {
		case 1:
			System.out.println("Set current time in a form hh:mm");
			answer = in.next();
			// Extract hours & minutes and change time
			hours = extractHours(answer);
			minutes = extractMinutes(answer);
		
			clock.setCurrentTime(new Time(hours, minutes));
			break;
		
		case 2:
			System.out.println("Set alarm time in a form hh:mm");
			answer = in.next();
			// Extract hours & minutes and change time
			hours = extractHours(answer);
			minutes = extractMinutes(answer);
			clock.setAlarmTime(new Time(hours, minutes));
			break;
			
		case 3:
			System.out.println("Type on or off");
			answer = in.next();
			clock.setAlarmSet(answer.equals("on"));
			break;
		
		
		default:
			System.out.println("Invalid option in the switch! Exiting");
			System.exit(-1);
			break;
		}
	}
	
	/**
	 * Extract hours in an integer format from a String in format hh:mm
	 * @param time Time in a hh:mm format
	 * @return Hours as an int
	 */
	private int extractHours(String time) {
		String temp = time.split(":")[0];
		return Integer.parseInt(temp);
	}
	
	/**
	 * Extract minutes in an integer format from a String in format hh:mm
	 * @param time Time in a hh:mm format
	 * @return Minutes as an int
	 */
	private int extractMinutes(String time) {
		String temp = time.split(":")[1];
		return Integer.parseInt(temp);		
	}
}
